#ifndef LOGIN_ECGEEG_H
#define LOGIN_ECGEEG_H

#include <QWidget>
#include "qchart.h"
#include "qserialport.h"
#include "qserialportinfo.h"
#include "QFileDevice"
#include "../charts/measurepagecharts.h"
#include "../dll/MultiUSBDongleLib.h"
// #include <fstream>  // for file I/O
#include <QtDataVisualization/Q3DSurface>
#include <QtDataVisualization/QSurfaceDataProxy>
#include "../charts/surfacegraphmodifier.h"
#include "tableView/tableviewheader.h"
#include <QStandardItemModel>
#include<QMediaPlayer>
#include <QtMultimedia>
#include <QVideoWidget>
#include <QTimer>
#include <QAudioOutput>
#include "beans/designdata.h"


namespace Ui {
class Login_ECGEEG;
}

class Login_ECGEEG : public QWidget
{
    Q_OBJECT
    enum COLUMN_NUM_E
    {
        COLUMN_NO= 0,
        COLUMN_TYPE,
        COLUMN_TIME,
        COLUMN_PATH,
        COLUMN_SOURCETYPE
    };

public:
    explicit Login_ECGEEG(QWidget *parent = nullptr);
    ~Login_ECGEEG();

protected slots:
    void onFFTAnaShow();
private slots:
    void on_login_bt_login_clicked();

    void on_pb_dataCollect_clicked();

    void on_pb_homepage_return_clicked();

    void on_pb_history_clicked();

    void on_pb_design_clicked();

    void on_pb_option_clicked();

    void on_pb_setting_return_clicked();

    void on_pb_OpenSerial_clicked();

    void on_pb_clearDevice_clicked();

    void readComDataSlot();

    void on_pb_bindDevice_clicked();

    void on_pb_conn_ecg_clicked();

    void on_pb_conn_eeg_clicked();

    void on_pb_measurepage_return_clicked();

    void on_pb_datacollect_start_clicked();

    void stopTrans();

    void on_comboBox_timeSet_currentIndexChanged(int index);

    void updateIndex();

    void updateDeviceState();

    void on_pb_login_close_clicked();

    void on_pb_historypage_return_clicked();

    void on_pb_designpage_return_clicked();

    void on_checkBox_ShadowShow_stateChanged(int arg1);

    void on_checkBox_drawWire_stateChanged(int arg1);

    void on_lineEdit_minY_textChanged(const QString &arg1);

    void on_checkBox_AdjustY_stateChanged(int arg1);

    void on_lineEdit_maxY_textChanged(const QString &arg1);


    void on_pb_configOver_clicked();

    void on_pb_designOver_clicked();

    void on_pb_routineAdd_clicked();


    void on_checkBox_video_toggled(bool checked);

    void on_checkBox_sound_toggled(bool checked);


    void on_pb_fileSelect_clicked();

    void on_pb_cijiAdd_clicked();

    void Mplayer_State(QMediaPlayer::MediaStatus enumState);
    void OnDurationChanged(qint64 i64Duration);
    void OnSlider(qint64 p);

    void on_comboBox_realtimeAnaSet_currentIndexChanged(int index);

    void on_comboBox_ffttimeSet_currentIndexChanged(int index);

    void on_pb_clearDesign_clicked();

    void on_checkBox_video_len_toggled(bool checked);

    void on_checkBox_video_len_dead_toggled(bool checked);

    void on_comboBox_designsele_currentTextChanged(const QString &arg1);

    void on_pb_deleteDesign_clicked();

    void onStepShow();
    void on_pb_Vidio_Stop_clicked();

    void on_horizontalSlider_volume_sliderMoved(int position);


    void onTabChanged(int index);
    void on_pb_browse_clicked();

    void on_pb_history_selectfile_clicked();

private:
    Ui::Login_ECGEEG *ui;
    QChart *ir_Chart,*ecg_Chart,*resp_Chart,*eeg_Chart;
    MeasurePageCharts *mpcs_ir,*mpcs_ecg,*mpcs_resp,*mpcs_eeg;
    QSerialPort *serialPort;
    void refresh_serial();
    bool isBind;
    bool isClear;
    bool StartCollectFlag,isSaveRawData;
    QByteArray Hexstring2QByteArray(QString str);
    unsigned char dualeegStartCollectOrder[13],dualeegStopCollectOrder[13];
    unsigned char ecgHolterStartCollectOrder[27],ecgHolterStopCollectOrder[27],ecgHolterInfoOrder[27];
    QTimer timer,timerRealTimeIndex,timerFFT3DAnaShow,timerForStep,timer_DeviceState;
    int stopNum;
    int SampleNum;
    int stepNum;
    int stepIndex;
    // std::ofstream outFile,outFile_UserInfo,outFile_Design;
    QTextStream stream,stream_info;
    QFile file;
    void getFileName();
    QString fileName,fileName_userInfo,fileName_design;
    Q3DSurface *m_graph;
    QWidget *container;
    SurfaceGraphModifier *modifier ;
    int tartetRow;
    int maxFFTMag=0;
    bool isAutoAdjugeY;
    void setFFTColorModel();

    void initTableWidget();
    void setColumnWidth();
    QStandardItemModel *m_pTableModel;
    QStandardItemModel *m_pTableModel_sel;
    QString selFileName;
    QSettings *settings;
    int totalTime;

    QVideoWidget *m_pPlayerWidget=nullptr;
    QMediaPlayer *m_pPlayer;
    QAudioOutput *audioOutput=nullptr;
    QString selVideoTime;
    bool isfirst;

    void initSettings();
    void initViedo();
    void initRealWaveCharts();
    void MultiUSBDongleAPI_Init();
    void initControls();
    void initTimers();
    void initSerialPort();
    void init3DFFT();
    void initVariables();
    void initWindowsSet();
    void initTableWidget_sel();
    void setColumnWidth_sel();
    QList<DesignData> getDesignData(QString s);
    void getIntTime(QString s);
    int convertTimeStringToSeconds(const QString &timeStr);
    QList<int>stepTimeDuration;//持续累积时间
    QList<int>eveyStepTime;//每个阶段的时间
    QList<int>switchFlag;
    QList<QString>resourcePath;
    void ExitCollect();
    bool isExitCollect;
    void Over();
    bool isEEGOff;
    bool isECGOff;
    void setConnectState(QLabel *lable, bool b);
    void deleteFile(QString fileName);

    QString CharArrayToHexString(QByteArray Buffer, int Len);
};

#endif // LOGIN_ECGEEG_H
